package com.bignerdranch.android.furnitureapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.bignerdranch.android.furnitureapplication.adapter.BrandInfoAdapter;
import com.bignerdranch.android.furnitureapplication.entity.BrandInfoBean;
import com.bignerdranch.android.furnitureapplication.net.RetrofitHelper;

import java.util.List;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class BrandInfoActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private List<BrandInfoBean> dataList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_brand_info);

        mRecyclerView = findViewById(R.id.brand_info_recycler);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        getBrandInfo();
    }

    private void getBrandInfo() {
        RetrofitHelper.getNewsService()
                .getBrandInfo()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<BrandInfoBean>>() {
                    @Override
                    public void onCompleted() {
                        BrandInfoAdapter adapter = new BrandInfoAdapter(dataList);
                        mRecyclerView.setAdapter(adapter);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(List<BrandInfoBean> brandInfoBeans) {
                        dataList = brandInfoBeans;
                    }
                });
    }
}
